﻿Option Explicit On
Option Strict On

Partial Class private_secure_admin_SingleUseTokenRequest
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Dim m As Membership
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        u = Membership.GetUser(User.Identity.Name)
        'txtSecQuestion.Text = u.PasswordQuestion
        'txtEmail.Text = u.Email
    End Sub
    Protected Sub btnTrialRegistrationClose_Click(sender As Object, e As System.EventArgs) Handles btnTrialRegistrationClose.Click
        If lblMsg.Text = "Noup, you can not create more shadow users today" Then
            TrialRegistrationSuccessModalPopupExtender.Hide()
        Else
            Response.Redirect("~/private/secure_members/default.aspx")
        End If
    End Sub
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        '' Create a new 12-character password with 1 non-alphanumeric character.
        'Dim status As MembershipCreateStatus
        'Dim password As String = Membership.GeneratePassword(12, 1)
        'u = Membership.GetUser(User.Identity.Name)
        'Try
        '    ' Create new user.
        '    Dim newUser As MembershipUser = Membership.CreateUser(TextBox1.Text, password, _
        '                                                          txtEmail.Text, txtSecQuestion.Text, txtSecAnswer.Text, True, status)

        '    lblMsg.Text = "User <b>" & Server.HtmlEncode(TextBox1.Text) & "</b> created. " & _
        '               "</br> Your temporary token is " & password & "</br> Your status is " & status.ToString() & "."

        'Catch ex As MembershipCreateUserException
        '    lblMsg.Text = GetErrorMessage(CType(ex.Message, MembershipCreateStatus))
        'Catch ey As HttpException
        '    lblMsg.Text = ey.Message
        'End Try
        Dim strMailFrom As String = ConfigurationManager.AppSettings("smtpmailfrom").ToString()

        Dim token As String = Membership.GeneratePassword(12, 0)
        Dim getpersonid As New GetPersonIdFromUserId()
        Dim personid As Integer = getpersonid.GetPersonId(CType(u.ProviderUserKey, Guid))

        Dim singletokenTA As New LogginsLogDSTableAdapters.usp_SingleTokenLoginTableAdapter()
        Dim singletokenDT As LogginsLogDS.usp_SingleTokenLoginDataTable
        singletokenDT = singletokenTA.GetData()
        Dim myactivetokens = From todaystokens In singletokenDT.AsEnumerable() Where todaystokens.PersonId = personid Select todaystokens

        For Each mytokensRow As LogginsLogDS.usp_SingleTokenLoginRow In myactivetokens
            Dim datetokenused As DateTime?
            If mytokensRow.IsDateTokenUsedNull Then
                datetokenused = Nothing
            Else
                datetokenused = mytokensRow.DateTokenUsed
            End If

            singletokenTA.Update(mytokensRow.PersonId, _
                                 mytokensRow.UserName, _
                                 mytokensRow.SingleUseToken, _
                                 datetokenused, _
                                 mytokensRow.DateTokenRequested, _
                                 mytokensRow.TokenValidForHours, _
                                 CType(0, Boolean?), _
                                 mytokensRow.Id, _
                                 mytokensRow.Id)
        Next
        Dim todaystokencount As Integer = (From todaystokens In singletokenDT.AsEnumerable() Where todaystokens.PersonId = personid AndAlso todaystokens.DateTokenRequested.ToShortDateString() = DateTime.Today.ToShortDateString() Select todaystokens).Count
        If todaystokencount < 2 Then
            singletokenTA.Insert(personid, txtUsername.Text, token, Nothing, DateTime.Now, 24, CType(1, Boolean?))
            Dim sendmail As New SendEmailViaGoogle()
            sendmail.SendEmail(strMailFrom, u.Email, "New trial user created", "New user created with username: " + txtUsername.Text + ", password: [" + token + "] and duration: " & 24 & " hours")
            lblMsg.Text = "User <b>" & Server.HtmlEncode(txtUsername.Text) & "</b> created. " & _
                       "</br> Your temporary password/token is: [" & token & "]."
            TrialRegistrationSuccessModalPopupExtender.Show()
        Else
            lblMsg.Text = "Noup, you can not create more shadow users today"
            TrialRegistrationSuccessModalPopupExtender.Show()
        End If
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    'Public Function GetErrorMessage(status As MembershipCreateStatus) As String

    '    Select Case status
    '        Case MembershipCreateStatus.DuplicateUserName
    '            Return "Username already exists. Please enter a different user name."

    '        Case MembershipCreateStatus.DuplicateEmail
    '            Return "A username for that e-mail address already exists. Please enter a different e-mail address."

    '        Case MembershipCreateStatus.InvalidPassword
    '            Return "The password provided is invalid. Please enter a valid password value."

    '        Case MembershipCreateStatus.InvalidEmail
    '            Return "The e-mail address provided is invalid. Please check the value and try again."

    '        Case MembershipCreateStatus.InvalidAnswer
    '            Return "The password retrieval answer provided is invalid. Please check the value and try again."

    '        Case MembershipCreateStatus.InvalidQuestion
    '            Return "The password retrieval question provided is invalid. Please check the value and try again."

    '        Case MembershipCreateStatus.ProviderError
    '            Return "The authentication provider Returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."

    '        Case MembershipCreateStatus.UserRejected
    '            Return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."

    '        Case Else
    '            Return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."
    '    End Select
    'End Function

    Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("~/private/default.aspx")
    End Sub
End Class
